package com.mctuoluo.mappay.sql;

import com.mctuoluo.mappay.MapPay;
import com.mctuoluo.supersql.CreatePool;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.sql.*;
import java.util.UUID;

public class update {
    private static MapPay mapPay = MapPay.getMapPay();
    //private static CreatePool createPool = new CreatePool();
    private static Connection conn ;
    private static Statement statement;
    private static ResultSet rs;


    /* private Connection connection = createPool.dataSource.getConnection(); */

    public update() throws SQLException {

    }

    public void sql(){
        try
        {
            conn = CreatePool.getHiDB();
            statement = conn.createStatement();
            statement.executeQuery("select count(*) from MapPay");
            mapPay.getLogger().info("已找到数据表不需要创建");
            conn.close();

        }
        catch(SQLException e)
        {
            sqls();
            System.out.print("尚未找到mysql数据表，正在创建数据表！");

        }
    }
    public void sqls(){

        try {
            conn = CreatePool.getHiDB();
            statement = conn.createStatement();
            statement.executeUpdate("CREATE TABLE IF NOT EXISTS `MapPay` (`player` varchar(64) NOT NULL,`id` varchar(64) NOT NULL,`points` varchar(10) DEFAULT NULL,PRIMARY KEY (`player`)) ENGINE=MyISAM DEFAULT CHARSET=gbk;");
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
    //添加改名卡
    public static boolean addCard(Player p,int i){
        try {
            conn = CreatePool.getHiDB();
            statement = conn.createStatement();
            String sql = "select * from MapPay where player='" + p.getPlayer().getUniqueId() + "' ";//查询玩家是否有数据！
            rs = statement.executeQuery(sql);
            if (!rs.next()) {
                sql = "INSERT INTO MapPay VALUES ('" + String.valueOf(p.getUniqueId()) + "', '" + p.getPlayer().getName() + "','"+i+"')";
                System.out.println(sql);
                statement.executeUpdate(sql);
                rs.close();
                statement.close();
                conn.close();
                return true;
            } else {
//                int am = rs.getInt("points");
//                rs.close();
//                sql = "UPDATE MapPay SET points = '" + String.valueOf(am+i) + "' WHERE player = '" + String.valueOf(p.getUniqueId()) + "'";
//                statement.executeUpdate(sql);
                statement.close();
                conn.close();
                return true;
            }
        } catch (SQLException e1) {
            e1.printStackTrace();
            return false;
        }
    }
    //扣除改名卡
    public static boolean takeCard(Player p,int i){

        try {
            conn = CreatePool.getHiDB();
            statement = conn.createStatement();
            String sql = "select * from MapPay where player='" + String.valueOf(p.getUniqueId()) + "' ";//查询玩家是否有数据！
            rs = statement.executeQuery(sql);
            if (!rs.next()) {
                rs.close();
                statement.close();
                conn.close();
                return true;
            } else {
                int am = rs.getInt("points");
                rs.close();
                sql = "UPDATE MapPay SET points = '" + String.valueOf(am-i) + "' WHERE player = '" + String.valueOf(p.getUniqueId()) + "'";
                statement.executeUpdate(sql);
                statement.close();
                conn.close();

                return true;
            }
        } catch (SQLException e1) {
            e1.printStackTrace();
            return false;
        }
    }

    //获取改名卡数量
    public static int getI(Player p){
        try {
            conn = CreatePool.getHiDB();
            statement = conn.createStatement();
            String sql = "select * from MapPay where player='" + String.valueOf(p.getUniqueId()) + "' ";//查询玩家是否有数据！
            rs = statement.executeQuery(sql);
            if (!rs.next()) {
                rs.close();
                statement.close();
                conn.close();
                return 0;
            } else {
                int am = rs.getInt("points");
                rs.close();
                statement.close();
                conn.close();
                return am;
            }
        } catch (SQLException e1) {
            e1.printStackTrace();

        }
        return -1;
    }
    //获取玩家原来的昵称
    public static String getID(Player p){
        try {
            conn = CreatePool.getHiDB();
            statement = conn.createStatement();
            String sql = "select * from MapPay where player='" + p.getPlayer().getUniqueId() + "' ";//查询玩家是否有数据！
            rs = statement.executeQuery(sql);
            if (!rs.next()) {
                rs.close();
                statement.close();
                conn.close();
                return p.getPlayer().getName();
            } else {
                String s = rs.getString("id");

                rs.close();
                statement.close();
                conn.close();
                if (s.equals("null")){
                    return p.getPlayer().getName();
                }
                return s;

            }
        } catch (SQLException e1) {
            e1.printStackTrace();
            return p.getPlayer().getName();
        }
    }

    public static String getID(UUID p){
        try {
            conn = CreatePool.getHiDB();
            statement = conn.createStatement();
            String sql = "select * from MapPay where player='" + p + "' ";//查询玩家是否有数据！
            rs = statement.executeQuery(sql);
            if (!rs.next()) {
                rs.close();
                statement.close();
                conn.close();
                return Bukkit.getPlayer(p).getName();
            } else {
                String s = rs.getString("id");

                rs.close();
                statement.close();
                conn.close();
                if (s.equals("null")){
                    return Bukkit.getPlayer(p).getName();
                }
                return s;

            }
        } catch (SQLException e1) {
            e1.printStackTrace();
            return Bukkit.getPlayer(p).getName();
        }
    }

    public static String getUUID(String p){
        try {
            conn = CreatePool.getHiDB();
            statement = conn.createStatement();
            String sql = "select * from MapPay where id='" + p + "' ";//查询玩家是否有数据！
            rs = statement.executeQuery(sql);
            if (!rs.next()) {
                rs.close();
                statement.close();
                conn.close();
                return Bukkit.getPlayer(p).getUniqueId().toString();
            } else {
                String s = rs.getString("player");

                rs.close();
                statement.close();
                conn.close();
                if (s.equals("null")){
                    return Bukkit.getPlayer(p).getUniqueId().toString();
                }
                return s;

            }
        } catch (SQLException e1) {
            e1.printStackTrace();
            return Bukkit.getPlayer(p).getUniqueId().toString();
        }
    }
    public static String getUUID(Player p){
        try {
            conn = CreatePool.getHiDB();
            statement = conn.createStatement();
            String sql = "select * from MapPay where id='" + p.getName() + "' ";//查询玩家是否有数据！
            rs = statement.executeQuery(sql);
            if (!rs.next()) {
                rs.close();
                statement.close();
                conn.close();
                return p.getUniqueId().toString();
            } else {
                String s = rs.getString("player");

                rs.close();
                statement.close();
                conn.close();
                if (s.equals("null")){
                    return p.getUniqueId().toString();
                }
                return s;

            }
        } catch (SQLException e1) {
            e1.printStackTrace();
            return p.getUniqueId().toString();
        }
    }
    //设置玩家昵称
//    public static boolean setName(Player p,String name) {
//
//        try {
//            conn = CreatePool.getHiDB();
//            statement = conn.createStatement();
//            String sql = "select * from namecard where player='" + String.valueOf(p.getUniqueId()) + "' ";//查询玩家是否有数据！
//            rs = statement.executeQuery(sql);
//            if (!rs.next()) {
//                sql = "select * from namecard where nick='" + name + "' ";//查询昵称是否有数据！
//                rs = statement.executeQuery(sql);
//                if (rs.next()) {
//                    p.sendMessage(PlaceholderAPI.setPlaceholders(p,LoadMessage.getString("IDrepeat")));
//                    return false;
//                }
//                sql = "INSERT INTO namecard VALUES ('" + String.valueOf(p.getUniqueId()) + "', '" + p.getPlayer().getName() + "','"+0+"','"+String.valueOf(p.getName())+"')";
//                statement.executeUpdate(sql);
//                rs.close();
//                statement.close();
//                conn.close();
//                p.sendMessage(PlaceholderAPI.setPlaceholders(p,LoadMessage.getString("setName").replaceAll("%changeName%",name)));
//                return true;
//            } else {
//                sql = "select * from namecard where nick='" + name + "' ";//查询昵称是否有数据！
//                rs = statement.executeQuery(sql);
//                if (rs.next()) {
//                    p.sendMessage(PlaceholderAPI.setPlaceholders(p,LoadMessage.getString("IDrepeat")));
//                    return false;
//                }
//                sql = "UPDATE namecard SET nick = '" + name + "' WHERE player = '" + String.valueOf(p.getUniqueId()) + "'";
//                statement.executeUpdate(sql);
//                statement.close();
//                conn.close();
//                //p.sendMessage("§8[§3§l改名卡§8] §e恭喜您成功修改了中文名！");
//                p.sendMessage(PlaceholderAPI.setPlaceholders(p,LoadMessage.getString("setName").replaceAll("%changeName%",name)));
//                setName.setName(p,name);
//                return true;
//            }
//        } catch (SQLException e1) {
//            e1.printStackTrace();
//            return false;
//        }
//
//    }
//    //获取玩家昵称
//    public static String getName(Player p){
//        try {
//            conn = CreatePool.getHiDB();
//            statement = conn.createStatement();
//            String sql = "select * from namecard where player='" + p.getPlayer().getUniqueId() + "' ";//查询玩家是否有数据！
//            rs = statement.executeQuery(sql);
//            if (!rs.next()) {
//                rs.close();
//                statement.close();
//                conn.close();
//                return p.getPlayer().getName();
//            } else {
//                String s = rs.getString("nick");
//
//                rs.close();
//                statement.close();
//                conn.close();
//                if (s.equals("null")){
//                    return p.getPlayer().getName();
//                }
//                return s;
//
//            }
//        } catch (SQLException e1) {
//            e1.printStackTrace();
//            return p.getPlayer().getName();
//        }
//    }


}